package com.example.zg.androidnewview;

import android.support.annotation.DrawableRes;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zg.androidnewview.fragments.Fragment1;
import com.example.zg.androidnewview.fragments.Fragment2;
import com.example.zg.androidnewview.fragments.Fragment3;
import com.example.zg.androidnewview.utils.IndicatorLineUtil;

import java.util.List;

public class TabLayout2Activity extends AppCompatActivity {
    private static final String TAG = "TabLayout2Activity";
    private TabLayout tabLayout;
    private String[] titles=new String[]{"最新","热门","我的"};
    private  @DrawableRes int[] tabRes=new int[]{R.drawable.img_audio,R.drawable.img_image,R.drawable.img_video};

    private FragmentManager fragmentManager;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment curFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout2);
        init();
    }

    private void init(){
        setCurFragment(0);
        tabLayout=findViewById(R.id.tabLayout2);
        //添加Tab
        for(int i=0;i<titles.length;i++){
            tabLayout.addTab(tabLayout.newTab().setCustomView(makeTabView(i)));
        }


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中tab
                Log.i(TAG, "onTabSelected: "+tab.getPosition());
                setCurFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab从选中状态到不选中状态
                Log.i(TAG, "onTabUnselected: "+tab.getPosition());

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //已经选中tab后重复点击
                Log.i(TAG, "onTabReselected: "+tab.getPosition());

            }
        });

    }


    private void setCurFragment(int position) {

        if (fragmentManager == null) {
            fragmentManager = getSupportFragmentManager();
        }
        Fragment newFragment = null;//要显示的Fragment
        switch (position) {
            case 0:
                if(fragment1==null){
                    fragment1 = fragmentManager.findFragmentByTag(Fragment1.class.getName());
                    if(fragment1==null){
                        fragment1=new Fragment1();
                    }
                }
                newFragment=fragment1;
                break;
            case 1:
                if(fragment2==null){
                    fragment2 = fragmentManager.findFragmentByTag(Fragment2.class.getName());
                    if(fragment2==null){
                        fragment2=new Fragment2();
                    }
                }
                newFragment=fragment2;
                break;
            case 2:
                if(fragment3==null){
                    fragment3 = fragmentManager.findFragmentByTag(Fragment3.class.getName());
                    if(fragment3==null){
                        fragment3=new Fragment3();
                    }
                }
                newFragment=fragment3;
                break;
        }

        FragmentTransaction ft = fragmentManager.beginTransaction();
        if(curFragment!=null){
            ft.hide(curFragment);
        }
        if(newFragment!=null){
            //新的页面已经被添加了则显示
            if(newFragment.isAdded()){
                ft.show(newFragment);
            }else{
                ft.add(R.id.frameLayout,newFragment,newFragment.getClass().getName());
            }
        }
        ft.commit();
        curFragment=newFragment;
        assert curFragment != null;
    }


    private View makeTabView(int position){
        View view = getLayoutInflater().inflate(R.layout.tab_view, null, false);
        ImageView imageView=view.findViewById(R.id.imageview) ;
        TextView textView=view.findViewById(R.id.textview);
        imageView.setImageResource(tabRes[position]);
        textView.setText(titles[position]);
        return view;
    }

}
